package com.no1.musicmgr.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {
    public static String formatDate(String input) {
        // 定义模式
        Pattern[] patterns = new Pattern[]{
                Pattern.compile("^(\\d{4})(\\d{1,2})(\\d{2})$"),  // yyyyMMdd 或 yyyyMdd 或 yyyyMMd 或 yyyyMd
                Pattern.compile("^(\\d{4})(\\d{1,2})$"),  // yyyyMM 或 yyyyM
                Pattern.compile("^(\\d{1,2})(\\d{2})$"),  // MMdd 或 Mdd
                Pattern.compile("^(\\d{4})[\\./](\\d{1,2})[\\./](\\d{1,2})$"),  // yyyy.MM.dd 或 yyyy.M.dd 或 yyyy.MM.d 或 yyyy.M.d
                Pattern.compile("^(\\d{1,2})[\\./](\\d{1,2})$"),  // MM.dd 或 M.dd 或 MM.d 或 M.d
                Pattern.compile("^(\\d{4})[\\./](\\d{1,2})$"),  // yyyy.MM 或 yyyy.M
                Pattern.compile("^(\\d{4})$"),  // yyyy
                Pattern.compile("^(\\d{1,2})$")  // M 或 MM
        };

        // 定义格式模板
        String[] formats = new String[]{
                "%s-%02d-%02d",  // 适用于 yyyyMMdd, yyyyMdd, yyyyMMd, yyyyMd
                "%s-%02d",  // 适用于 yyyyMM 或 yyyyM
                "%02d-%02d",  // 适用于 MMdd 或 Mdd
                "%s-%02d-%02d",  // 适用于 yyyy.MM.dd, yyyy.M.dd, yyyy.MM.d, yyyy.M.d
                "%02d-%02d",  // 适用于 MM.dd, M.dd, MM.d, M.d
                "%s-%02d",  // 适用于 yyyy.MM 或 yyyy.M
                "%s",  // 适用于 yyyy
                "%02d"  // 适用于 M 或 MM
        };

        for (int i = 0; i < patterns.length; i++) {
            Matcher matcher = patterns[i].matcher(input);
            if (matcher.matches()) {
                switch (i) {
                    case 0:
                        return String.format(formats[i], matcher.group(1), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)));
                    case 1:
                        return String.format(formats[i], matcher.group(1), Integer.parseInt(matcher.group(2)));
                    case 2:
                        return String.format(formats[i], Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)));
                    case 3:
                        return String.format(formats[i], matcher.group(1), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)));
                    case 4:
                        return String.format(formats[i], Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)));
                    case 5:
                        return String.format(formats[i], matcher.group(1), Integer.parseInt(matcher.group(2)));
                    case 6:
                        return String.format(formats[i], matcher.group(1));
                    case 7:
                        return String.format(formats[i], Integer.parseInt(matcher.group(1)));
                }
            }
        }

        // 如果没有匹配模式，则返回原字符串
        return input;
    }

    public static void main(String[] args) {
        // 测试用例
        String[] testInputs = {
                "20240823", "2024823", "0823", "823",
                "2024.08.23", "2024.8.23", "08.23", "8.23",
                "2024/08/23", "2024/8/23", "08/23", "8/23",
                "202407", "20247","2024"
        };

        for (String input : testInputs) {
            System.out.println(input + " -> " + formatDate(input));
        }
    }
}
